package com.wisewill.wakeonlan.common;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;


public class AppService {
	public static <T> T get(Object v, int id){
		try {
			return (T)v.getClass().getMethod("findViewById", Integer.TYPE).invoke(v, id);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static void alert(Context ctx, CharSequence msg){
		AlertDialog alert;
		alert = new AlertDialog.Builder(ctx).create();
		alert.setButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		alert.setMessage(msg);
		alert.show();
	}
	
	public static String readMac(){
		String mac = PropertyUtil.getProp("mac");
		if(mac == null)
			mac = "";
		else
			mac = mac.trim();
		return mac;
	}
	
	public static String readIp(){
		String wakeIp = PropertyUtil.getProp("wakeIp");
		if(wakeIp == null)
			wakeIp = "";
		else
			wakeIp = wakeIp.trim();
		if(wakeIp.length() <= 0)
			wakeIp = "255.255.255.255";
		
		return wakeIp;
	}
	
	public static void save(String mac, String wakeIp){
		PropertyUtil.setProp("mac", mac);
		PropertyUtil.setProp("wakeIp", wakeIp);
	}
}
